from utils.baseResource import BaseResource
from flask_restful import request, reqparse
from utils.db_seesion import fetch_to_dict, execute
from utils.Response import response
from utils.db_seesion import fetch_to_dict_pagetion

import time


class HostAdd(BaseResource):
    paser = reqparse.RequestParser()
    # 校验必传参数，hostname和host
    paser.add_argument('hostname', required=True, help='host_name is required')
    paser.add_argument('host', required=True, help='host is required')

    def post(self):
        HostAdd.paser.parse_args()
        host_name = self._datas.get('hostname')
        host = self._datas.get('host')
        host_desc = self._datas.get('hostdesc')
        create_user = self._user_id

        # 判定host_name是否重名
        sql_check_host_name = 'select host_name from host where host_name = :host_name'
        check_host_name_result = fetch_to_dict(sql_check_host_name, params={'host_name': host_name}, fecth='one')
        if check_host_name_result:
            return response(code=1001, msg='host名称已存在')

        create_time = time.time()
        update_time = time.time()

        print('host_name' + host_name + 'host' + host + 'host_desc' + host_desc + 'create_user' + create_user)
        print(create_time)
        # 添加新的host
        sql = "insert into host(host_name,host,host_desc,create_time,create_user,update_time,isdelete,isavailable) values (:host_name,:host,:host_desc,:create_time,:create_user,:update_time,:isdelete,:isavailable)"

        execute(sql=sql,
                params={'host_name': host_name, 'host': host, 'host_desc': host_desc, 'create_time': create_time,
                        'create_user': create_user, 'update_time': update_time, 'isdelete': 0, 'isavailable': 0})

        return response(msg='添加新host成功')


class HostEdit(BaseResource):
    paser = reqparse.RequestParser()
    # 校验必传参数，hostname和host
    paser.add_argument('hostid', required=True, help='host_id is required')
    paser.add_argument('hostname', required=True, help='host_name is required')
    paser.add_argument('host', required=True, help='host is required')

    def post(self):
        HostEdit.paser.parse_args()
        id = self._datas.get('hostid')
        host_name = self._datas.get('hostname')
        host = self._datas.get('host')
        host_desc = self._datas.get('hostdesc')

        update_time = time.time()

        # 修改host
        sql_update_host = 'update host set host_name = :host_name,host = :host,host_desc = :host_desc,update_time = :update_time where id = :id'

        execute(sql=sql_update_host,
                params={'host_name': host_name, 'host': host, 'host_desc': host_desc, 'update_time': update_time,
                        'id': id})

        return response(msg='修改host成功')


class HostDel(BaseResource):
    paser = reqparse.RequestParser()
    # 校验必须项
    paser.add_argument('hostid', required=True, help='host_id is required')

    def post(self):
        # 通过id删除
        HostEdit.paser.parse_args()
        id = self._datas.get('hostid')

        #判定是否已经被删除
        sql_check_host_isdelete = 'select id from host where id = :id and isdelete = :isdelete'
        check_host_isdelete_result = fetch_to_dict(sql=sql_check_host_isdelete,params={'id':id,'isdelete':1})
        if check_host_isdelete_result:
            return response(msg='已经删除了')

        update_time = time.time()
        ###
        # 修改isdelete字段值，0表示可用，1表示删除
        sql_delhost = 'update host set isdelete = 1,update_time = :update_time where id = :id'

        execute(sql=sql_delhost,
                params={'update_time':update_time,'id':id})

        return response(msg='修改host成功')




#host列表和模糊查询
class HostList(BaseResource):
    paser = reqparse.RequestParser()

    def get(self):
        HostList.paser.parse_args()
        #模糊查询字段
        keyword = request.args.get('keyword')
        page = request.args.get('page')
        pagesize = request.args.get('size')
        #模糊查询
        if keyword:
            sql_dimhostlist = 'select a.id,a.host_name,a.host,a.host_desc,a.create_time,b.nickname as create_user,a.update_time from host a inner join users b on a.create_user = b.id where a.host_name like :keyword and isdelete =0'
            result = fetch_to_dict_pagetion(sql_dimhostlist,params={'keyword':"%{}%".format(keyword)}, page=page,page_size=pagesize)
            if result:
                return response(msg='查询成功',data=result)
        #
        else:
            sql_hostlist = 'select a.id,a.host_name,a.host,a.host_desc,a.create_time,b.nickname as create_user,a.update_time from host a inner join users b on a.create_user = b.id where isdelete =0'
            result = fetch_to_dict_pagetion(sql_hostlist,page=page,page_size=pagesize)
            return response(msg='查询成功',data=result)
